package com.nameli.admin.service.impl;


import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nameli.SIM.bean.entity.CheckMajorInfo;
import com.nameli.SIM.bean.entity.MajorInfo;
import com.nameli.SIM.bean.query.MajorInfoQuery;
import com.nameli.SIM.bean.vo.MajorInfoVO;
import com.nameli.SIM.mapper.CheckMajorInfoMapper;
import com.nameli.SIM.mapper.MajorInfoMapper;
import com.nameli.SIM.util.EmptyUtil;
import com.nameli.SIM.util.RedisUtil;
import com.nameli.admin.service.MajorService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author NAME-L
 * @Description TODO
 * @className MajorServiceImpl
 * @date 2021-10-31 17:41:42
 */
@Component
@Transactional(rollbackFor = {Exception.class}, timeout = 3)
public class MajorServiceImpl implements MajorService {

    private static final Log log = LogFactory.getLog(MajorServiceImpl.class);
    @Autowired
    private MajorInfoMapper majorMapper;

    @Autowired
    private CheckMajorInfoMapper checkMajorInfoMapper;

    @Autowired
    private RedisUtil redisUtil;

    @Override
    public int queryAllCount() {
        return majorMapper.queryAllCount();
    }


    @Override
    public PageInfo<MajorInfoVO> queryAllMajor(Integer pageIndex , Integer pageSize) {
        PageHelper.startPage(pageIndex, pageSize);
        List<MajorInfoVO> majorInfoVOS = majorMapper.queryAllmajor();
        log.info("output -----------> majorInfoVOS:" + majorInfoVOS);
        PageInfo<MajorInfoVO> majorInfoPageInfo = new PageInfo<>(majorInfoVOS);
        return majorInfoPageInfo;
    }

    @Override
    public PageInfo<MajorInfoVO> queryByCondition(MajorInfoQuery majorInfoQuery , Integer pageIndex , Integer pageSize ) {
        if( EmptyUtil.isNotEmpty(pageIndex) && EmptyUtil.isNotEmpty(pageSize)){
            if(EmptyUtil.isNotEmpty(majorInfoQuery)){
                PageHelper.startPage(pageIndex, pageSize);
                List<MajorInfoVO> majorInfos = majorMapper.queryByCondition(majorInfoQuery);
                PageInfo<MajorInfoVO> majorInfoPageInfo = new PageInfo<>(majorInfos);
                return majorInfoPageInfo;
            }
        }
        return null;
    }

    @Override
    public Boolean updateMajorInfo(MajorInfo majorInfo) {
        log.info("enter into -----------> MajorServiceImpl - updateMajorInfo");
        if(EmptyUtil.isNotEmpty(majorInfo)){
            String methodType = "2";//修改操作
            String majorNo = majorInfo.getMajorNo();//获取修改的专业信息编号
            log.info("要修改的专业编号 - majorNo:" + majorNo);
            MajorInfo beforeMajor = majorMapper.queryOneByMajorNo(majorNo);
            Boolean addflag = redisUtil.set("update_"+majorNo, beforeMajor);
            if (addflag){
                this.insertCheckMajor(majorInfo , methodType);
                Boolean flag = majorMapper.updateMajorInfo(majorInfo);
                log.info("专业更新结果 flag:"+flag);
                return flag;
            }
        }
        return false;
    }

    @Override
    public List<Map<String, Object>> queryByFacultyNo(String facultyNo) {
        List<Map<String, Object>> returnList = majorMapper.queryByFacultyNo(facultyNo);
        return returnList;
    }

    @Override
    public int queryCountByFacultyNo(String facultyNo) {
        return majorMapper.queryCountByFacultyNo(facultyNo);
    }

    @Override
    public int selectMaxId() {
        return majorMapper.selectMaxId();
    }

    @Override
    public Boolean insertMajor(MajorInfo majorInfo) {
        log.info("enter into -----------> MajorServiceImpl - insertMajor");
        String methodType = "1";//新增操作
        this.insertCheckMajor(majorInfo , methodType);
        Boolean flag = majorMapper.insertMajor(majorInfo);
        if (flag) {
            String key = "insert_"+majorInfo.getMajorNo();
            boolean addflag = redisUtil.set(key, majorInfo);
            log.info("新增数据插入redis addflag:" + addflag);
        }
        log.info("添加专业信息结果 - flag:" + flag);
        return flag;
    }

    @Override
    public Boolean updateConfirmCheck(String majorNo , String status) {
        log.info("enter into -----------> MajorServiceImpl - updateConfirmCheck");
        Integer checkStatus = null;
        if ("5".equals(status)){
            checkStatus = 5;
        }else {
            checkStatus = 1;
        }
        Boolean flag =majorMapper.updateConfirmCheck(majorNo , checkStatus);
        log.info("flag:" + flag);
        return flag;
    }

    /**
     * 通用审核记录添加操作
     * @param majorInfo
     * @param methodType
     */
    public void insertCheckMajor(MajorInfo majorInfo , String methodType) {
        log.info("enter into -----------> MajorServiceImpl - insertCheckMajor");
        log.info("进入专业审核记录添加");
        CheckMajorInfo checkMajorInfo = BeanUtil.toBean(majorInfo, CheckMajorInfo.class);
        log.info("checkMajorInfo:"+checkMajorInfo);
        Integer maxId = checkMajorInfoMapper.selectMaxId();
        if (maxId == null){
            maxId = 1;
            checkMajorInfo.setId(maxId);
        }else{
            checkMajorInfo.setId(maxId + 1);
        }
        log.info("审核编号maxId:" + maxId);
        checkMajorInfo.setMethodType(Integer.valueOf(methodType));//methodType为1则是新增操作，为2则是修改操作
        checkMajorInfo.setCheckStatus(2);//2 - 待审核状态
        log.info("checkMajorInfo:" + checkMajorInfo);
        Boolean checkFlag = checkMajorInfoMapper.insertMajorCheck(checkMajorInfo);
        log.info("添加专业审核结果 - checkFlag:" + checkFlag);
    }
}
